package cn.jiguang.jgadpubadapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import java.util.List;
import java.util.Map;

import cn.jiguang.jgssp.ad.data.ADJgNativeAdInfo;
import cn.jiguang.jgssp.ad.data.ADJgNativeExpressAdInfo;
import cn.jiguang.jgssp.ad.data.ADJgNativeFeedAdInfo;
import cn.jiguang.jgssp.util.ADJgAdUtil;
import cn.jiguang.jgssp.util.ADJgViewUtil;
import xyz.adscope.amps.ad.nativead.adapter.AMPSNativeAdExpressListener;
import xyz.adscope.amps.ad.unified.adapter.AMPSUnifiedNativeAdapter;
import xyz.adscope.amps.ad.unified.inter.AMPSAppDetail;
import xyz.adscope.amps.ad.unified.inter.AMPSBaseTransformEntry;
import xyz.adscope.amps.ad.unified.inter.AMPSUnifiedPattern;
import xyz.adscope.amps.ad.unified.inter.AMPSUnifiedVideoListener;
import xyz.adscope.amps.ad.unified.inter.AMPSVideoConfig;
import xyz.adscope.amps.ad.unified.view.AMPSUnifiedMediaViewStub;
import xyz.adscope.amps.ad.unified.view.AMPSUnifiedRootContainer;

public class JgNativeEntry extends AMPSBaseTransformEntry {

    private ADJgNativeAdInfo adJgNativeAdInfo;
    private AMPSNativeAdExpressListener expressListener;

    public JgNativeEntry(Context context, ADJgNativeAdInfo adJgNativeAdInfo,
                         int ecpm, AMPSVideoConfig config, AMPSUnifiedNativeAdapter adapter) {
        super(context, ecpm, config, adapter);
        this.adJgNativeAdInfo = adJgNativeAdInfo;
    }

    public ADJgNativeAdInfo getADJgInfo(){
        return  adJgNativeAdInfo;
    }


    @Override
    public boolean isValid() {
        if (adJgNativeAdInfo == null) {
            return false;
        }
        return true;
    }

    private static final String TAG = "JgNativeEntry";

    @Override
    public boolean isExpressAd() {
        return adJgNativeAdInfo.isNativeExpress();
    }

    @Override
    public void render() {
        LogUtil.d(TAG, "entry render: " );
        super.render();
        if (adJgNativeAdInfo == null) {
            return;
        }
        //模版广告的渲染
        if (adJgNativeAdInfo.isNativeExpress()) {
            if (!ADJgAdUtil.adInfoIsRelease(adJgNativeAdInfo)) {

                expressListener.onRenderSuccess(getNativeExpressAdView(),0,0);
            }
        }
    }
    FrameLayout frameLayout;

    @Override
    public View getNativeExpressAdView() {
        LogUtil.d(TAG, "getNativeExpressAdView: ");
        if (adJgNativeAdInfo == null) {
            return null;
        }
        //自渲染的才有
        if (adJgNativeAdInfo.isNativeExpress()) {
            ADJgNativeExpressAdInfo expressAdInfo = ((ADJgNativeExpressAdInfo) adJgNativeAdInfo);
            if (!ADJgAdUtil.adInfoIsRelease(adJgNativeAdInfo)) {
                if (frameLayout == null) {
                    frameLayout = new FrameLayout(mContext);
                }
                ADJgViewUtil.addAdViewToAdContainer(frameLayout,expressAdInfo.getNativeExpressAdView(frameLayout));
                expressAdInfo.render(frameLayout);
                return frameLayout;
            }
        }
        return null;
    }

    public AMPSNativeAdExpressListener getExpressListener(){
        return expressListener;
    }

    @Override
    public void setNativeAdExpressListener(AMPSNativeAdExpressListener ampsNativeAdExpressListener) {
        super.setNativeAdExpressListener(ampsNativeAdExpressListener);
        this.expressListener = ampsNativeAdExpressListener;
    }


    @Override
    public String getTitle() {
        if (adJgNativeAdInfo == null) {
            return null;
        }
        //自渲染的才有
        if (adJgNativeAdInfo.isNativeExpress()) {
            return null;
        } else {
            return ((ADJgNativeFeedAdInfo) adJgNativeAdInfo).getTitle();
        }
    }

    @Override
    public String getDesc() {
        if (adJgNativeAdInfo == null) {
            return null;
        }
        //自渲染的才有
        if (adJgNativeAdInfo.isNativeExpress()) {
            return null;
        } else {
            return ((ADJgNativeFeedAdInfo) adJgNativeAdInfo).getDesc();
        }
    }

    @Override
    public String getActionButtonText() {
        if (adJgNativeAdInfo == null) {
            return null;
        }
        //自渲染的才有
        if (adJgNativeAdInfo.isNativeExpress()) {
            return null;
        } else {
            return ((ADJgNativeFeedAdInfo) adJgNativeAdInfo).getCtaText();
        }
    }

    //获取广告扩展信息
    @Override
    public Map<String, Object> getExtras() {
        return super.getExtras();
    }

    @Override
    public String getAdSourceLogoUrl() {
        if (adJgNativeAdInfo == null) {
            return null;
        }
        //自渲染的才有
        if (adJgNativeAdInfo.isNativeExpress()) {
            return null;
        } else {
            return String.valueOf(((ADJgNativeFeedAdInfo) adJgNativeAdInfo).getPlatformIcon());
        }
    }


    @Override
    public String getIconUrl() {
        if (adJgNativeAdInfo == null) {
            return null;
        }
        //自渲染的才有
        if (adJgNativeAdInfo.isNativeExpress()) {
            return null;
        } else {
            return ((ADJgNativeFeedAdInfo) adJgNativeAdInfo).getIconUrl();
        }
    }

    @Override
    public boolean isViewObject() {
        return adJgNativeAdInfo.isNativeExpress();
    }

    @Override
    public String getMainImageUrl() {
        if (adJgNativeAdInfo == null) {
            return null;
        }
        //自渲染的才有
        if (adJgNativeAdInfo.isNativeExpress()) {
            return null;
        } else {
            return ((ADJgNativeFeedAdInfo) adJgNativeAdInfo).getImageUrl();
        }
    }

    @Override
    public AMPSUnifiedPattern getAdPattern() {
        return super.getAdPattern();
    }


    @Override
    public int getECPM() {
        if (adJgNativeAdInfo == null) {
            return 0;
        }
        return (int) adJgNativeAdInfo.getECPM();
    }

    @Override
    public AMPSAppDetail getAppDetail() {
        return this;
    }

    @Override
    public void bindAdToRootContainer(Activity context, AMPSUnifiedRootContainer rootView, List<View> clickViews, List<View> actionView) {
        super.bindAdToRootContainer(context, rootView, clickViews, actionView);
        Log.e(TAG, "bindAdToRootContainer: " );
        if (adJgNativeAdInfo != null) {
            if (adJgNativeAdInfo.isNativeExpress()) {
                if (frameLayout == null) {
                    frameLayout = new FrameLayout(mContext);
                }
                ADJgViewUtil.addAdViewToAdContainer(rootView, ((ADJgNativeExpressAdInfo) adJgNativeAdInfo).getNativeExpressAdView(frameLayout));
            }
        }
    }

    @Override
    public void bindAdToMediaView(Activity context, AMPSUnifiedMediaViewStub stub, AMPSUnifiedVideoListener listener) {
        if (adJgNativeAdInfo == null || stub == null) {
            return;
        }
        if (adJgNativeAdInfo.isNativeExpress()) {
            if (((ADJgNativeExpressAdInfo) adJgNativeAdInfo).isVideo()) {
                LogUtil.d(TAG, "bindAdToMediaView: " );
            }
        }
    }

    @Override
    public void resume() {
        super.resume();
    }

    @Override
    public void pause() {
        super.pause();
    }

    @Override
    public void destroy() {
        if (adJgNativeAdInfo != null) {
            adJgNativeAdInfo = null;
        }
        if (frameLayout != null) {
            frameLayout = null;
        }
        super.destroy();
    }
}
